<?php
declare (strict_types = 1);

namespace app\index\model;

use think\facade\Config;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class Order extends Model
{
    protected $pk='order_id';
    use SoftDelete;
    protected $deleteTime = 'del';
    public function searchOrderSnAttr($query,$value,$data){
        if ("" !== $value){
            $query->where('order_sn', 'like', '%' . $value . '%');
        }
    }
    public function searchOrderAmountMinAttr($query,$value,$data){
        if ("" !== $value){
            $query->where('order_amount', '>=', $value);
        }
    }
    public function searchOrderAmountMaxAttr($query,$value,$data){
        if ("" !== $value){
            $query->where('order_amount', '<=', $value);
        }
    }
    public function searchStatusIdAttr($query,$value,$data){
        if (is_array($value) &&count($value) > 0|| (is_scalar($value) &&  strpos('#'.$value,','))){
                $query->whereIn('status_id', $value);
        }
        if(is_scalar($value) && false===strpos($value,',') && intval($value)<2){
            $query->where('status_id',intval($value));
        }
    }
    public function getAddDate($value,$data){
        return date('Y-m-d H:i:s',$value);
    }

    public static function getOrder($data = [], $query = [], $page = false)
    {

        $field = [
            "buyer_name",
            "buyer_id",
            "order_amount",
            "status",
            "add_time",
            "postscript",
            "order_id",
            "order_sn",
            "s.order_status" => "order_statuss",
            "FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%S')" => 'add_date',
        ];
        $model = self::alias("o")
            ->join(['ecm_order_status' => 's'], 'o.status=s.status_id')
            ->field($field)
            ->order("add_date desc")
            ->withSearch(array_keys($data),$data);



        $list_rows = Config::has('view.list_rows') ? Config::get('view.list_rows') : 15;

        if ($page) {
            $result = $model->select();
        } else {

            $result = $model->paginate([
                'list_rows' => $list_rows,
                'fragment' => 'top',
                'query' => $query,
            ])->each(function ($item) use ($data) {
                if (!empty($data['keywords'])) {
                    $item['order_sn'] =

                        str_replace($data['keywords'], '<span style="color: red;font-weight: 900">' . $data['keywords'] . '</span>', $item['order_sn']);
                    return $item;
                }
            });
            //
        }
        return $result;
    }

}

//$field = [
//    "buyer_name",
//    "buyer_id",
//    "order_amount",
//    "status",
//    "add_time",
//    "postscript",
//    "order_id",
//    "order_sn",
//    "s.order_status"=>"order_statuss",
//    "FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%S')" => 'add_date',
//];

//$db = Db::table("ecm_order")
//    ->alias("o")
//    ->join(['ecm_order_status'=>'s'],'o.status=s.status_id')
//    ->field($field)
//    ->order("add_date desc");
////订单号模糊搜索
//if(""!==$order_sn){
//    $db->where('order_sn','like','%'.$order_sn.'%');
//}
//View::assign('order_sn',$order_sn);
//
//if($order_amount !==""){
//    $db->where('order_amount','>=',$order_amount);
//
//}
//View::assign('order_amount',$order_amount);
//
//if($order_amount1 !==""){
//    $db->where('order_amount','<=',$order_amount1);
//}
//View::assign('order_amount1',$order_amount1);
//
//if(count($status_id) >0){
//    $db->where(function ($q)use ($status_id){
//        $q->whereIn('status',$status_id);
//    });
//
//}
//
//View::assign('status_id',$status_id);
//
//
//$dd=$db->paginate([
//    'list_rows'=>6,
////                'var_page'=>'p',
////                'page' =>3,
//    'fragment'=>'top',
//    'query'=>input('get.'),
//]);
//$page=$dd->render();
//View::assign('page',$page);
//$dd=$dd->toArray()['data'];
//foreach ($dd as $k=>$v){
//    $dd[$k]['order_sn']=str_replace($order_sn,'<span style="color: red;font-weight: 900">'.$order_sn.'</span>',$v['order_sn']);
//}